﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UsingEvent
{
    public partial class Form1 : Form
    {
        //1.建立事件函數
        private void MyButton_Click(object sender, EventArgs e)
        {
            Console.WriteLine("事件發生了喔");
        }

        //2.委派宣告
        public delegate void ClickEventHandler(object sender, EventArgs e);
        //3.創建MyBottom類別
        public class MyButton
        {
            //4.宣告MyBottom的事件
            public event ClickEventHandler ClickEvent;
            //5.建立MyButton成員方法
            public void Click()
            {
                if (ClickEvent != null)
                {
                    Console.WriteLine("事件開始");
                    //6.拋出事件，給所有相應者
                    ClickEvent(this, null);
                }
            }
        }

        //7.引用類別實體化
        public MyButton myButton = new MyButton();    
        public Form1()
        {
            InitializeComponent();
            //8.事件實體化
            myButton.ClickEvent += new ClickEventHandler(MyButton_Click);

        }

        //使用方式
        private void Form1_Load(object sender, EventArgs e)
        {
            //引用
            Form1 form = new Form1();
            form.myButton.Click();
            
        }



    }
}